package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.SceneCommentSQuery;
import cn.wolfcode.luowowo.article.query.SceneQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.ISceneStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.SceneStatisVO;
import cn.wolfcode.luowowo.comment.domain.SceneCommentReply;
import cn.wolfcode.luowowo.comment.query.SceneCommentReplyQuery;
import cn.wolfcode.luowowo.comment.service.ISceneCommentReplyService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.DataFormatException;

@Controller
@RequestMapping("scene")
public class SceneController {

    @Reference
    private ISceneService sceneService;
    @Reference
    private ISceneSurveyService sceneSurveyService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ISceneCommentService sceneCommentService;
    @Reference
    private ISceneStatisRedisService sceneStatisRedisService;
    @Reference
    private ISceneThemeService sceneThemeService;
    @Reference
    private ISceneTagService sceneTagService;
    @Reference
    private ISceneCommentReplyService sceneCommentReplyService;
    //景点列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SceneQuery qo){
        //toasts
        List<Destination> toasts = destinationService.queryForParent(qo.getDestId());
        toasts.remove(0);
        Destination dest = toasts.get(1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //景点概况
        model.addAttribute("scene",sceneSurveyService.selectDestBysurvey(qo.getDestId()));

        //热门景点排名前十的景点
        List<Scene> hots = sceneService.selectTop10();
        if(hots.size()>0){
            model.addAttribute("hot",hots);
        }
        //点评数量前五的景点
        List<Scene> selectHotTop5 = sceneService.selectHotTop5(qo.getDestId());
        model.addAttribute("selectHotTop5",selectHotTop5);
        //主题查询
        List<SceneTheme> themes=sceneThemeService.getTemeByScene(qo.getDestId());
        model.addAttribute("themes",themes);

        return "scene/list";
    }
    @RequestMapping("detail")
    public String detail(Model model, @ModelAttribute("qo") SceneCommentSQuery qo,@UserParam UserInfo userInfo){
        //根据景点id查询当前景点
        Scene scene = sceneService.selectScene(qo.getSceneId());
        model.addAttribute("s",scene);
        int i = sceneCommentService.selectReply(qo.getSceneId());
        model.addAttribute("i",i);
        //查询景点的景点
        List<Scene> list=sceneService.selectListByScene(qo.getSceneId());
        model.addAttribute("scenes",list);
        //toasts
        List<Destination> toasts = destinationService.queryForParent(scene.getDests().getId());
        toasts.remove(0);
        Destination dest = toasts.get(1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);


        //收藏
        List<Long> c = sceneStatisRedisService.getSceneStatisFavor(qo.getSceneId());
        model.addAttribute("c",c);
        //去过
        List<Long> g = sceneStatisRedisService.getSceneStatisGoNum(qo.getSceneId());
        model.addAttribute("g",g);
        //景点标签
        List<SceneTag> tags = sceneTagService.selectForTag(qo.getSceneId());
        model.addAttribute("tags",tags);



        // userInfo
        model.addAttribute("userInfo", userInfo);

        return "scene/detail";
    }


    @RequestMapping("pinglun")
    public String pinglun(Model model,@ModelAttribute("qo")SceneCommentSQuery qo){
        //获取评论分页
        PageInfo<SceneComments> pageInfo=sceneCommentService.query(qo);

        //获取评论集合
        List<SceneComments> list = pageInfo.getList();
        List<SceneComments> lists = new ArrayList<>();
        for (SceneComments sceneComments : list) {

            SceneCommentReplyQuery scrqo = new SceneCommentReplyQuery();
            scrqo.setSceneCommentId(sceneComments.getId());
            scrqo.setCurrentPage(1);
            scrqo.setPageSize(Integer.MAX_VALUE);
            List<SceneCommentReply> content = sceneCommentReplyService.query(scrqo).getContent();
            sceneComments.setSceneCommentReplies(content);
            lists.add(sceneComments);
        }

        pageInfo.setList(lists);

        model.addAttribute("pageInfo",pageInfo);

        return "scene/pinglun";
    }


    //收藏
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        //通过ret确定返回什么类型的值
        boolean ret = sceneStatisRedisService.favor(sid, userInfo.getId());

        SceneStatisVO vo = sceneStatisRedisService.getSceneStatisVO(sid);

        return new AjaxResult(ret).getData(vo.getFavornum());
    }
    //去过
    @RequestMapping("gotonum")
    @ResponseBody
    public Object gotonum(Long qid, @UserParam UserInfo userInfo) {

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        //通过ret确定返回什么类型的值
        boolean ret = sceneStatisRedisService.gotonum(qid, userInfo.getId());
        SceneStatisVO vo = sceneStatisRedisService.getSceneStatisVO(qid);
        return new AjaxResult(ret).getData(vo.getGoNum());
    }

    @RequestMapping("scene")
    public String scene(@ModelAttribute("qo")SceneQuery qo,Model model){
        PageInfo<Scene> pageInfo = sceneService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "scene/scene";
    }

    //添加评论回复
    @RequestMapping("/commentReplyAdd")
    public String commentAdd(SceneCommentReply comment, @UserParam UserInfo userInfo,Model model){


        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());

        SceneCommentReply cr = sceneCommentReplyService.saveOrUpdate(comment);

        System.out.println(cr);

        model.addAttribute("s", cr);
        model.addAttribute("userInfo",userInfo);

        return "scene/replyTpl";
    }

    //添加评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(SceneComments comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setCreateTime(new Date());
        sceneCommentService.saveOrUpdate(comment);
        return new AjaxResult();
    }


}

